
package system.base;

import framework.core.*;
import system.data.*;
/*
	Author    :Wing
	CreateTime:2008-02-20 18:11:02
*/
public class OutSubSrv extends IHandler
{

	public OutSubSrv() 
	{
		super();
	}
	private void deleteOutSub(DBConnection con,SrvContext context) throws Exception
	{
		String[] ids = context.getParameterValues("ids");
		OutSub obj = null;
		for(int i=0;i<ids.length;i++)
		{
			obj = OutSub.fetch(con,ids[i]);
			if(obj!=null) obj.delete(con);
		}
	}
	private void saveOutSub(DBConnection con,SrvContext context,ILoginContext loginContext) throws Exception
	{
		String id = context.getString("Id",null);
		OutSub obj;
		if((id==null)||(id.equals("")))   //add
		{
			obj = OutSub.fetch();
    	}
    	else                     //update
    	{
    		obj = OutSub.fetch(con,id);
    	}
		OutSubPageData data = (OutSubPageData)PageData.fromForm(context,OutSubPageData.class);
		context.setValue("OutSubPageData",data);
		obj.fill(data);
		obj.save(con);
	}
	private void listOutSub(DBConnection con,SrvContext context,String path) throws Exception
	{
		OutSubSearchEvent event = (OutSubSearchEvent)SearchEvent.fromForm(context,OutSubSearchEvent.class);
		context.setSessionValue("OutSubSearchEvent",event);
		context.setNextUrl(path+"OutSubList.jsp");
	}
	private void backOutSub(DBConnection con,SrvContext context,String path) throws Exception
	{
		context.setNextUrl(path+"OutSubList.jsp");
	}
	
	public void doAction(DBConnection con,SrvContext context,ILoginContext loginContext) throws Exception
	{
		String action_code = context.getActionCode();
		String path = "/system/base/";
		
		if(action_code.equalsIgnoreCase("listOutSub"))
		{
			listOutSub(con,context,path);
			return;
		}
		if(action_code.equalsIgnoreCase("viewOutSub"))
		{
			String id = context.getString("Id",null);
			OutSub obj = OutSub.fetch(con,id);
			context.setValue("OutSub",obj);
			context.setNextUrl(path+"OutSubView.jsp");
			return;
		}
		if(action_code.equalsIgnoreCase("deleteOutSub"))
		{
			context.setErrorUrl(path+"OutSubList.jsp");
			deleteOutSub(con,context);
			backOutSub(con,context,path);
			return;
		}
			
		if(action_code.equalsIgnoreCase("toAddOutSub"))
		{
			OutSub obj = OutSub.fetch();
			OutSubPageData data = (OutSubPageData)PageData.fromTable(obj,OutSubPageData.class);
			context.setValue("OutSubPageData",data);
			context.setNextUrl(path+"OutSubEdit.jsp");
			return;
		}
		if(action_code.equalsIgnoreCase("toUpdateOutSub"))
		{
			String id = context.getString("Id",null);
			OutSub obj = OutSub.fetch(con,id);
			OutSubPageData data = (OutSubPageData)PageData.fromTable(obj,OutSubPageData.class);
			context.setValue("OutSubPageData",data);
			context.setNextUrl(path+"OutSubEdit.jsp");
			return;
		}
		if(action_code.equalsIgnoreCase("toCopyOutSub"))
		{
			String id = context.getString("Id",null);
			OutSub obj = OutSub.copy(OutSub.fetch(con,id));
			OutSubPageData data = (OutSubPageData)PageData.fromTable(obj,OutSubPageData.class);
			context.setValue("OutSubPageData",data);
			context.setNextUrl(path+"OutSubEdit.jsp");
			return;
		}
		if(action_code.equalsIgnoreCase("saveOutSub"))
		{
			context.setErrorUrl(path+"OutSubEdit.jsp");
			saveOutSub(con,context,loginContext);
			backOutSub(con,context,path);
			return;
		}
		if(action_code.equalsIgnoreCase("saveAndNewOutSub"))
		{
			context.setErrorUrl(path+"OutSubEdit.jsp");
			saveOutSub(con,context,loginContext);
			OutSub obj = OutSub.fetch();
			OutSubPageData data = (OutSubPageData)PageData.fromTable(obj,OutSubPageData.class);
			context.setValue("OutSubPageData",data);
			context.setNextUrl(path+"OutSubEdit.jsp");			
			return;
		}
		if(action_code.equalsIgnoreCase("backListOutSub"))
		{
			backOutSub(con,context,path);
			return;
		}
		
		//add by Wing start

		//add by Wing end

		Logger.throwException("OutSubSrv","unknown action_code="+action_code);
	}
}
